package com.example.micro_property_springboot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.micro_property_springboot.entity.SysNotificationSetting;
import com.example.micro_property_springboot.mapper.SysNotificationSettingMapper;
import com.example.micro_property_springboot.pojo.vo.NotificationSettingVO;
import com.example.micro_property_springboot.service.SysNotificationSettingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 系统通知设置服务实现
 */
@Service
public class SysNotificationSettingServiceImpl extends ServiceImpl<SysNotificationSettingMapper, SysNotificationSetting>
        implements SysNotificationSettingService {
    
    @Autowired
    private SysNotificationSettingMapper notificationSettingMapper;
    
    @Override
    public List<SysNotificationSetting> getByUserId(Long userId) {
        return notificationSettingMapper.getByUserId(userId);
    }
    
    @Override
    @Transactional
    public boolean saveOrUpdateSetting(NotificationSettingVO settingVO) {
        // 查询是否已存在
        SysNotificationSetting existing = notificationSettingMapper.getByUserIdAndType(
                settingVO.getUserId(), settingVO.getNotifyType(), settingVO.getEventType());
        
        if (existing != null) {
            // 更新现有设置
            existing.setEnabled(settingVO.getEnabled());
            existing.setUpdateTime(new Date());
            return updateById(existing);
        } else {
            // 创建新设置
            SysNotificationSetting newSetting = new SysNotificationSetting();
            BeanUtils.copyProperties(settingVO, newSetting);
            newSetting.setCreateTime(new Date());
            newSetting.setUpdateTime(new Date());
            return save(newSetting);
        }
    }
    
    @Override
    @Transactional
    public boolean batchSaveSettings(List<NotificationSettingVO> settingVOs) {
        if (settingVOs == null || settingVOs.isEmpty()) {
            return true;
        }
        
        List<SysNotificationSetting> settings = new ArrayList<>();
        Date now = new Date();
        
        for (NotificationSettingVO vo : settingVOs) {
            SysNotificationSetting existing = notificationSettingMapper.getByUserIdAndType(
                    vo.getUserId(), vo.getNotifyType(), vo.getEventType());
            
            if (existing != null) {
                existing.setEnabled(vo.getEnabled());
                existing.setUpdateTime(now);
                settings.add(existing);
            } else {
                SysNotificationSetting newSetting = new SysNotificationSetting();
                BeanUtils.copyProperties(vo, newSetting);
                newSetting.setCreateTime(now);
                newSetting.setUpdateTime(now);
                settings.add(newSetting);
            }
        }
        
        // 使用MyBatis-Plus的批量保存或更新功能
        return saveOrUpdateBatch(settings);
    }
    
    @Override
    public List<SysNotificationSetting> getEnabledSettingsByEvent(Long userId, String eventType) {
        return notificationSettingMapper.getEnabledByUserIdAndEvent(userId, eventType);
    }
    
    @Override
    @Transactional
    public void initDefaultSettings(Long userId) {
        // 定义默认的通知类型和事件类型组合
        String[] notifyTypes = {"system", "email"};
        String[] eventTypes = {"approve", "overdue", "system_announce"};
        
        Date now = new Date();
        List<SysNotificationSetting> defaultSettings = new ArrayList<>();
        
        for (String notifyType : notifyTypes) {
            for (String eventType : eventTypes) {
                // 检查是否已存在
                SysNotificationSetting existing = notificationSettingMapper.getByUserIdAndType(
                        userId, notifyType, eventType);
                
                if (existing == null) {
                    // 默认启用所有通知
                    SysNotificationSetting setting = new SysNotificationSetting();
                    setting.setUserId(userId);
                    setting.setNotifyType(notifyType);
                    setting.setEventType(eventType);
                    setting.setEnabled(true);
                    setting.setCreateTime(now);
                    setting.setUpdateTime(now);
                    defaultSettings.add(setting);
                }
            }
        }
        
        if (!defaultSettings.isEmpty()) {
            saveBatch(defaultSettings);
        }
    }
}